package com.maszy.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.maszy.bean.Action;
import com.maszy.service.ActionService;


public class ActionServlet  extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	ActionService service = new ActionService();
	String view = new String(); // 页面路径
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		ActionServlet servlet = new ActionServlet();
		RequestDispatcher rd = null;
		String method = request.getParameter("method");		
		if ("queryAll".equals(method)) {
			// 查询出全部数据
			servlet.queryAll(request, response);
			view = "/background/action/action.jsp";
		} 
		else if ("insert".equals(method)) {
			// 添加新记录
			servlet.add(request, response);
			servlet.queryAll(request, response);
			view = "/background/action/action.jsp";
		} else if ("update".equals(method)) {
			// 修改记录
			servlet.update(request, response);
			servlet.queryAll(request, response);
			view = "/background/action/action.jsp";
		} else if ("delete".equals(method)) {
			// 删除记录
			servlet.delete(request, response);
			servlet.queryAll(request, response);
			view = "/background/action/action.jsp";
		} else if ("updateForward".equals(method)) {
			// 进入修改页面
			servlet.queryForOne(request, response);
			view = "/background/action/update_action.jsp";
		} 

		try {
			rd = this.getServletContext().getRequestDispatcher(view);
			rd.forward(request, response);

		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void queryForOne(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		String id=request.getParameter("Action_Id");
		if(!"".equals(id)&&id!=null){
			Action obj=new Action();
			obj.setAction_Id(Long.parseLong(id));
			obj=service.queryById(obj);
			request.setAttribute("obj", obj);
		}
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String id=request.getParameter("Action_Id");
		if(!"".equals(id)&&id!=null){
			Action obj=new Action();
			obj.setAction_Id(Long.parseLong(id));
			service.delete(obj);
		}
	}

	private void update(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		Action obj = new Action();
		String id=request.getParameter("Action_Id");
		if(!"".equals(id)&&id!=null){		
			obj.setAction_Id(Long.parseLong(id));		
			obj.setAction_Name(request.getParameter("Action_Name"));
			obj.setAction_Remark(request.getParameter("Action_Remark"));
			service.update(obj);
		}
	}

	private void queryAll(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		List<Action> list=service.queryAll();
		request.setAttribute("list", list);
	}

	private void add(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		Action obj = new Action();
		String id=request.getParameter("Action_Id");
		if(!"".equals(id)&&id!=null){		
			obj.setAction_Id(Long.parseLong(id));		
			obj.setAction_Name(request.getParameter("Action_Name"));
			obj.setAction_Remark(request.getParameter("Action_Remark"));
			service.add(obj);
		}

	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		doGet(request, response);
	}
}